package me.wangjian.redis.controller.rest;

import me.wangjian.redis.model.Company;
import me.wangjian.redis.service.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.UriComponentsBuilder;

import java.util.List;

/**
 * Created by wangjian on 2016/11/17.
 */
@RestController
public class CompanyController {

    @Autowired
    CompanyService companyService;


    @RequestMapping(value = "/companies/",method = RequestMethod.GET)
    public ResponseEntity<List<Company>> list(){
        List<Company> companies = companyService.findAll();
        if(companies.isEmpty()){
            return new ResponseEntity<List<Company>>(HttpStatus.NO_CONTENT);
        }
        return new ResponseEntity<List<Company>>(companies,HttpStatus.OK);
    }

    @RequestMapping(value="/companies/", method=RequestMethod.POST)
    public ResponseEntity<Void> postCompany(@RequestBody Company company, UriComponentsBuilder ucBuilder){
        Company saved = companyService.save(company);
        HttpHeaders headers = new HttpHeaders();
        headers.setLocation(ucBuilder.path("/companies/{id}").buildAndExpand(saved.getId()).toUri());
        return new ResponseEntity<Void>(headers, HttpStatus.CREATED);
    }

    @RequestMapping(value="/companies/{id}", method=RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Company> getCompany(@PathVariable Long id){
        Company company = companyService.findById(id);
        if(null==company){
            return new ResponseEntity<Company>(HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<Company>(company,HttpStatus.OK);
    }

    @RequestMapping(value="/companies/{id}", method=RequestMethod.PUT)
    public ResponseEntity<Company> putCompany(@PathVariable Long id,@RequestBody Company company){
        Company com = companyService.findById(id);

        if (com==null) {
            System.out.println("Company with id " + id + " not found");
            return new ResponseEntity<Company>(HttpStatus.NOT_FOUND);
        }

        com.setName(company.getName());
        com.setAddress(company.getAddress());
        com.setService(company.getService());
        companyService.save(com);
        return new ResponseEntity<Company>(com, HttpStatus.OK);
    }

    @RequestMapping(value="/companies/{id}", method=RequestMethod.DELETE)
    public ResponseEntity<Company> deleteCompany(@PathVariable Long id) {
        Company company = companyService.findById(id);
        if (company==null) {
            System.out.println("Company with id " + id + " not found");
            return new ResponseEntity<Company>(HttpStatus.NOT_FOUND);
        }
        companyService.remove(id);
        return new ResponseEntity<Company>(HttpStatus.NO_CONTENT);
    }

}
